﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Security;
using System.Threading;
using System.Windows.Forms;
using BlueMarineWindowsFormsClient.ManagerService;
using MarkupConverter;
using Microsoft.Reporting.WinForms;

namespace BlueMarineWindowsFormsClient
{
    public partial class frmHotelManager : Form
    {
        private readonly ImageTool _imageTool = new ImageTool();
        private readonly Status _status;
        private readonly frmMain _parent;
        private ManagerServiceClient _hotel;        

        public frmHotelManager(frmMain parent)
        {
            _parent = parent;
            InitializeComponent();

            Temp_EquipmentDetails = new List<EquipmentDetail>();
            MinimumSize = Size;
            ptbPromotionBanner.SizeMode = PictureBoxSizeMode.Zoom;
            ptbPromotionImage.SizeMode = PictureBoxSizeMode.Zoom;
            InitServiceClient();
            
            _status = new Status(lblStatus);
        }

        public override sealed Size MinimumSize
        {
            get { return base.MinimumSize; }
            set { base.MinimumSize = value; }
        }

        public List<EquipmentDetail> Temp_EquipmentDetails { get; set; }

        private void InitServiceClient()
        {
            _hotel = new ManagerServiceClient("WSHttpBinding_IManagerService")
                         {
                             ClientCredentials =
                                 {
                                     UserName = {UserName = _parent.UName, Password = _parent.PWord},
                                     ServiceCertificate =
                                         {
                                             Authentication =
                                                 {CertificateValidationMode = X509CertificateValidationMode.None}
                                         }
                                 }
                         };
        }

        private void AbortServiceClient()
        {
            if (_hotel.State != CommunicationState.Faulted) return;
            _hotel.Abort();
            InitServiceClient();
        }

        private void tabmain_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (hotelManager.SelectedIndex)
            {
                case 0:
                    ViewEquipment();
                    ViewSupply();
                    break;
                case 1:
                    ViewRoomType();
                    ViewRoom();
                    FillCbxRoomType();
                    break;
                case 2:
                    ViewPromotion();
                    break;
            }
        }

        private void RoomManagement_Load(object sender, EventArgs e)
        {
            ViewEquipment();
            ViewSupply();            
        }

        private void frmHotelManager_FormClosed(object sender, FormClosedEventArgs e)
        {
            _parent.Dispose();
        }

        private void employeeManagerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (_parent.Emp == null)
            {
                _parent.Emp = new frmEmployeeManager(_parent);
                _parent.Emp.Show();
                Hide();
            }
            else
            {
                _parent.Emp.Show();
                Hide();
            }
        }

        private void logoutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var t = new Thread(ThreadProc);
            t.Start();
            Close();
        }

        public void ThreadProc()
        {
            Application.Run(new frmMain(_parent.UName, ""));
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var about = new frmAbout();

            about.ShowDialog(this);
        }

        private void aboutUserToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var userinf = new frmUserInfo(_parent);
            userinf.ShowDialog(this);
        }

        #region EQUIPMEMT/SUPPLY

        private void ViewEquipment(int i = 0)
        {
            try
            {
                List<EquipmentDetail> data = _hotel.EquipmentDetailsGet(chbxViewAvaiEq.Checked);
                var datasource = new BindingSource();
                switch (dtgEquipment.Columns[i].Name)
                {
                    case "EquipmentDetailID":
                        datasource.DataSource = data;
                        break;
                    case "EquipmentName":
                        datasource.DataSource = data.OrderBy(p => p.EquipmentName);
                        break;
                    case "EquipmentPrice":
                        datasource.DataSource = data.OrderBy(p => p.EquipmentPrice);
                        break;
                    default:
                        return;
                }

                dtgEquipment.DataSource = datasource;
            }
            catch (Exception ex)
            {
                _status.ERROR(ex.Message);
            }
        }

        private void ViewSupply(int i = 0)
        {
            try
            {
                List<SupplyDetail> data = _hotel.SupplyDetailsGet(chbxViewAvaiSu.Checked);
                var datasource = new BindingSource();
                switch (dtgSupply.Columns[i].Name)
                {
                    case "SupplyDetailID":
                        datasource.DataSource = data;
                        break;
                    case "SupplyName":
                        datasource.DataSource = data.OrderBy(p => p.SupplyName);
                        break;
                    case "SupplyPrice":
                        datasource.DataSource = data.OrderBy(p => p.SupplyPrice);
                        break;
                    default:
                        return;
                }
                dtgSupply.DataSource = datasource;
            }
            catch (Exception ex)
            {
                _status.ERROR(ex.Message);
            }
        }

        #region Eqipment

        private void btnAddEq_Click(object sender, EventArgs e)
        {
            try
            {
                _status.STATUS("Writting Equipment...");
                if (_hotel.EquipmentDetailAdd(txtEqName.Text, decimal.Parse(txtEqPrice.Text),
                                              chbxAvailableEquipment.Checked))
                {
                    ViewEquipment();
                    _status.SUCCESS("Add Equipment");
                    return;
                }
                _status.FAIL("Add Equipment");
            }
            catch (Exception ex)
            {
                AbortServiceClient();
                _status.ERROR(ex.Message);
            }
        }

        private void dtgEquipment_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (e.RowIndex > -1)
                {
                    int id = int.Parse(dtgEquipment.Rows[e.RowIndex].Cells["EquipmentDetailID"].Value.ToString());
                    EquipmentDetail equipmentDetail = _hotel.EquipmentDetailGetById(id);
                    if (equipmentDetail == null) return;
                    txtEqID.Text = equipmentDetail.EquipmentDetailID.ToString();
                    txtEqName.Text = equipmentDetail.EquipmentName;
                    txtEqPrice.Text = equipmentDetail.EquipmentPrice.ToString();
                    chbxAvailableEquipment.Checked = equipmentDetail.Available;
                }
                else
                {
                    int id = e.ColumnIndex;
                    ViewEquipment(id);
                }
            }
            catch (Exception ex)
            {
                AbortServiceClient();
                _status.ERROR(ex.Message);
            }
        }

        private void chbxViewAvaiEq_CheckedChanged(object sender, EventArgs e)
        {
            ViewEquipment();
        }

        private void btnUpdateEq_Click(object sender, EventArgs e)
        {
            try
            {
                _status.STATUS("Writting Equipment...");
                if (_hotel.EquipmentDetailModify(int.Parse(txtEqID.Text), txtEqName.Text,
                                           decimal.Parse(txtEqPrice.Text),
                                           chbxAvailableEquipment.Checked))
                {
                    _status.SUCCESS("Update Equipment");
                    ViewEquipment();
                    return;
                }
                _status.FAIL("Update Equipment");
            }
            catch (Exception ex)
            {
                AbortServiceClient();
                _status.ERROR(ex.Message);
            }
        }

        #endregion

        #region Supply

        private void btnAddSu_Click(object sender, EventArgs e)
        {
            try
            {
                _status.STATUS("Writting Supply Detail...");
                if (_hotel.SupplyDetailAdd(chbxAvailableSupply.Checked, txtSupplyName.Text,
                                           decimal.Parse(txtSupplyPrice.Text), txtSupplyUnit.Text))
                {
                    _status.SUCCESS("Add Supply Detail");
                    ViewSupply();
                    return;
                }
                _status.FAIL("Add Supply Detail");
            }
            catch (Exception ex)
            {
                AbortServiceClient();
                _status.ERROR(ex.Message);
            }
        }

        private void btnSearchSupply_Click(object sender, EventArgs e)
        {
            try
            {
                _status.STATUS("Searching Supply Detail...");
                List<SupplyDetail> data = _hotel.SupplyDetailsGetByName(txtSupplyName.Text);
                dtgSupply.DataSource = new BindingSource {DataSource = data};
                _status.SUCCESS("Search Supply Detail...");
            }
            catch (Exception ex)
            {
                _status.ERROR(ex.Message);
            }
        }

        private void btnUpdateSu_Click(object sender, EventArgs e)
        {
            try
            {
                _status.STATUS("Writting Supply Detail...");
                if (_hotel.SupplyDetailModify(int.Parse(txtSupplyId.Text), chbxAvailableSupply.Checked,
                                              txtSupplyName.Text, decimal.Parse(txtSupplyPrice.Text),
                                              txtSupplyUnit.Text))
                {
                    _status.SUCCESS("Add Supply Detail");
                    ViewSupply();
                    return;
                }
                _status.FAIL("Add Supply Detail");
            }
            catch (Exception ex)
            {
                AbortServiceClient();
                _status.ERROR(ex.Message);
            }
        }

        private void chbxViewAvaiSu_CheckedChanged(object sender, EventArgs e)
        {
            ViewSupply();
        }

        private void dtgSupply_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (e.RowIndex > -1)
                {
                    SupplyDetail supplyDetail =
                        _hotel.SupplyDetailGetById(
                            int.Parse(dtgSupply.Rows[e.RowIndex].Cells[0].Value.ToString()));
                    txtSupplyName.Text = supplyDetail.SupplyName;
                    txtSupplyId.Text = supplyDetail.SupplyDetailID.ToString();
                    txtSupplyPrice.Text = supplyDetail.SupplyPrice.ToString();
                    txtSupplyUnit.Text = supplyDetail.Unit;
                    chbxAvailableSupply.Checked = supplyDetail.Available;
                }
                else
                {
                    int id = e.ColumnIndex;
                    ViewSupply(id);
                }
            }
            catch (Exception ex)
            {
                _status.ERROR(ex.Message);
            }
        }

        #endregion

        #endregion

        #region ROOMTYPE/ROOM

        #region Roomtype

        private void ViewRoomType()
        {
            List<RoomType> data = _hotel.RoomTypeGets();
            dtgRoomType.DataSource = new BindingSource {DataSource = data};
        }

        public void viewRoomTypeEquipment()
        {
            var source = new BindingSource {DataSource = Temp_EquipmentDetails};
            dtgRoTyEq.DataSource = source;
        }

        private void dtgRoomType_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (dtgRoomType.CurrentRow == null) return;
                int id = int.Parse(dtgRoomType.Rows[e.RowIndex].Cells[0].Value.ToString());
                Temp_EquipmentDetails = _hotel.EquipmentDetailsGetByRoomTypeID(id).ToList();
                RoomType roomType = _hotel.RoomTypeGetById(id);
                txtRoTyID.Text = roomType.RoomTypeID.ToString();
                txtRoTyTitle.Text = roomType.RoomTypeTitle;
                txtRoTyRank.Text = roomType.RoomRank;
                txtRoTyPrice.Text = roomType.RoomPrice.ToString();
                chbxBalcony.Checked = roomType.Balcony;
                chbxBath.Checked = roomType.Bath;
                chbxView.Checked = roomType.View;

                viewRoomTypeEquipment();
            }
            catch (Exception ex)
            {
                _status.ERROR(ex.Message);
            }
        }

        private void btnAddRoTyEq_Click(object sender, EventArgs e)
        {
            var equipmentSelect = new frmEquipmentSelect(this, _parent.UName, _parent.PWord);
            equipmentSelect.Show();
        }

        private void btnRemoveRoTyEq_Click(object sender, EventArgs e)
        {
            try
            {
                DataGridViewSelectedRowCollection idxs = dtgRoTyEq.SelectedRows;
                for (int i = 0; i < idxs.Count; i++)
                {
                    int idx = idxs[i].Index;
                    Temp_EquipmentDetails.RemoveAt(idx);
                }
                viewRoomTypeEquipment();
            }
            catch (Exception ex)
            {
                _status.ERROR(ex.Message);
            }
        }

        private void btnAddRoomType_Click(object sender, EventArgs e)
        {
            try
            {
                _status.STATUS("Writting Room Type...");
                var ids = new List<int>();
                for (int i = 0; i < dtgRoTyEq.Rows.Count; i++)
                {
                    ids.Add(int.Parse(dtgRoTyEq.Rows[i].Cells[0].Value.ToString()));
                }
                if (_hotel.RoomTypeAdd(txtRoTyTitle.Text, txtRoTyRank.Text,
                                       decimal.Parse(txtRoTyPrice.Text),
                                       chbxBath.Checked,
                                       chbxView.Checked, chbxBalcony.Checked, ids))
                {
                    _status.SUCCESS("Add Room Type");
                    ViewRoomType();
                    FillCbxRoomType();       
                    return;
                }
                _status.FAIL("Add Room Type");
            }
            catch (Exception ex)
            {
                AbortServiceClient();
                _status.ERROR(ex.Message);
            }
        }

        private void btnUpdateRoomType_Click(object sender, EventArgs e)
        {
            try
            {
                _status.STATUS("Writting Room Type...");
                var ids = new List<int>();
                for (int i = 0; i < dtgRoTyEq.Rows.Count; i++)
                {
                    ids.Add(int.Parse(dtgRoTyEq.Rows[i].Cells[0].Value.ToString()));
                }


                if (_hotel.RoomTypeModify(int.Parse(txtRoTyID.Text), txtRoTyTitle.Text,
                                          txtRoTyRank.Text,
                                          decimal.Parse(txtRoTyPrice.Text),
                                          chbxBath.Checked, chbxView.Checked, chbxBalcony.Checked, ids))
                {
                    _status.SUCCESS("Update Room Type");
                    ViewRoomType();
                    viewRoomTypeEquipment();
                    FillCbxRoomType();
                    return;
                }
                _status.FAIL("Update Room Type");
            }
            catch (Exception ex)
            {
                AbortServiceClient();
                _status.ERROR(ex.Message);
            }
        }

        #endregion

        #region Room

        private void ViewRoom()
        {
            try
            {
                int stt = 1;
                if (radRoomOccupied.Checked)
                {
                    stt = 0;
                }
                else if (radRoomUnavailable.Checked)
                {
                    stt = -1;
                }
                List<Room> data = _hotel.RoomsGet(stt);
                var source = new BindingSource {DataSource = data};
                dtgRoom.DataSource = source;
            }
            catch (Exception ex)
            {
                _status.ERROR(ex.Message);
            }
        }

        private void FillCbxRoomType()
        {
            try
            {
                cbxRoomType.Items.Clear();
                List<RoomType> data = _hotel.RoomTypeGets();
                foreach (RoomType roomType in data)
                {
                    cbxRoomType.Items.Add(roomType);
                }
                cbxRoomType.DisplayMember = "RoomTypeTitle";
                cbxRoomType.ValueMember = "RoomTypeID";
                if (data.Count > 0)
                {
                    cbxRoomType.SelectedIndex = 0;
                }
            }
            catch (Exception ex)
            {
                AbortServiceClient();
                _status.ERROR(ex.Message);
            }
        }

        private void dtgRoom_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (dtgRoom.CurrentRow != null)
                {
                    int id = int.Parse(dtgRoom.Rows[e.RowIndex].Cells[0].Value.ToString());
                    Room room = _hotel.RoomGetById(id);
                    txtRoomId.Text = room.RoomID.ToString(CultureInfo.InvariantCulture);
                    switch (room.Status)
                    {
                        case 0:
                            radRoomOccupied.Checked = true;
                            break;
                        case 1:
                            radRoomAvailable.Checked = true;
                            break;
                        default:
                            radRoomUnavailable.Checked = true;
                            break;
                    }
                    for (int i = 0; i < cbxRoomType.Items.Count; i++)
                    {
                        var roomType = (RoomType) cbxRoomType.Items[i];
                        if (roomType.RoomTypeID == room.RoomTypeID)
                        {
                            cbxRoomType.SelectedIndex = i;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _status.ERROR(ex.Message);
            }
        }

        private void btnAddRoom_Click(object sender, EventArgs e)
        {
            try
            {
                _status.STATUS("Writting Room...");
                var roomType = (RoomType) cbxRoomType.SelectedItem;
                string roomTypeId = roomType.RoomTypeID.ToString(CultureInfo.InvariantCulture);
                string state = "-1";
                if (!radRoomUnavailable.Checked)
                {
                    state = !radRoomOccupied.Checked ? "1" : "0";
                }
                if (_hotel.RoomAdd(int.Parse(txtRoomId.Text), int.Parse(state),
                                   int.Parse(roomTypeId)))
                {
                    _status.SUCCESS("Add Room");
                    ViewRoom();
                    return;
                }
                _status.FAIL("Add Room");
            }
            catch (Exception ex)
            {
                AbortServiceClient();
                _status.ERROR(ex.Message);
            }
        }

        private void btnUpdateRoom_Click(object sender, EventArgs e)
        {
            try
            {
                _status.STATUS("Writting Room...");
                var roomType = (RoomType) cbxRoomType.SelectedItem;
                string roomTypeId = roomType.RoomTypeID.ToString(CultureInfo.InvariantCulture);
                string state = "-1";
                if (!radRoomUnavailable.Checked)
                {
                    state = !radRoomOccupied.Checked ? "1" : "0";
                }
                if (_hotel.RoomModify(int.Parse(txtRoomId.Text), int.Parse(state),
                                      int.Parse(roomTypeId)))
                {
                    _status.SUCCESS("Update Room");
                    ViewRoom();
                    return;
                }
                _status.FAIL("Update Room");
            }
            catch (Exception ex)
            {
                AbortServiceClient();
                _status.ERROR(ex.Message);
            }
        }


        private void btnSearchStatus_Click(object sender, EventArgs e)
        {
            ViewRoom();
        }

        private void btnSearchEquipment_Click(object sender, EventArgs e)
        {
            try
            {
                List<EquipmentDetail> data = _hotel.EquipmentDetailsGetByName(txtEqName.Text);
                dtgEquipment.DataSource = new BindingSource {DataSource = data};
            }
            catch (Exception ex)
            {
                _status.ERROR(ex.Message);
            }
        }

        private void btnSeachByRoomTypeTitle_Click(object sender, EventArgs e)
        {
            try
            {
                var roomType = (RoomType) cbxRoomType.SelectedItem;
                int id = roomType.RoomTypeID;
                List<Room> data = _hotel.RoomsGetByRoomTypeId(id);
                dtgRoom.DataSource = new BindingSource {DataSource = data};
            }
            catch (Exception ex)
            {
                _status.ERROR(ex.Message);
            }
        }

        private void btnSeachRoom_Click(object sender, EventArgs e)
        {
            try
            {
                Room room = _hotel.RoomGetById(int.Parse(txtRoomId.Text));
                if (room == null)
                {
                    _status.STATUS(string.Format("No room name {0}", txtRoomId.Text));
                    return;
                }
                txtRoomId.Text = room.RoomID.ToString(CultureInfo.InvariantCulture);
                switch (room.Status)
                {
                    case 0:
                        radRoomOccupied.Checked = true;
                        break;
                    case 1:
                        radRoomAvailable.Checked = true;
                        break;
                    default:
                        radRoomUnavailable.Checked = true;
                        break;
                }
                for (int i = 0; i < cbxRoomType.Items.Count; i++)
                {
                    var roomType = (RoomType) cbxRoomType.Items[i];
                    if (roomType.RoomTypeID == room.RoomTypeID)
                    {
                        cbxRoomType.SelectedIndex = i;
                    }
                }
            }
            catch (Exception ex)
            {
                _status.ERROR(ex.Message);
            }
        }

        #endregion

        #endregion

        #region PROMOTION

        private void ViewPromotion()
        {
            try
            {
                var data = _hotel.PromotionsGet(chbxAvailablePromotion.Checked);
                dtgPromotion.DataSource = data;
            }
            catch (Exception ex)
            {
                _status.ERROR(ex.Message);
            }
        }


        private void dtgPromotion_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (e.RowIndex<0) return;
                int promotionId =
                    int.Parse(dtgPromotion.Rows[e.RowIndex].Cells[0].Value.ToString());
                lblPromotionId.Text = promotionId.ToString();
                Promotion promotion = _hotel.PromotionGetById(promotionId);
                txtDescription.Text = promotion.PromotionDescription;
                txtPromotionTitle.Text = promotion.PromotionTitle;
                dtpStart.Value = promotion.PromotionStart;
                dtpEnd.Value = promotion.PromotionEnd;

                ptbPromotionBanner.Image = _imageTool.ByteToImage(promotion.PromotionBanner);
                txtPromotionBanner.Text = "";

                ViewPromotionDetail();
            }
            catch (Exception ex)
            {
                _status.ERROR(ex.Message);
            }
        }

        private void ptbPromotionBanner_Click(object sender, EventArgs e)
        {
            try
            {
                var ofd = new OpenFileDialog {Filter = "JPG file|*.jpg|JPEG file|*.jpeg"};
                if (ofd.ShowDialog() != DialogResult.OK) return;
                Image img = Image.FromFile(ofd.FileName);
                ptbPromotionBanner.Image = img;
                txtPromotionBanner.Text = ofd.FileName;
            }
            catch (Exception ex)
            {
                _status.ERROR(ex.Message);
            }
        }

        private void ptbPromotionImage_Click(object sender, EventArgs e)
        {
            try
            {
                var ofd = new OpenFileDialog {Filter = "JPG file|*.jpg|JPEG file|*.jpeg"};
                if (ofd.ShowDialog() != DialogResult.OK) return;
                Image img = Image.FromFile(ofd.FileName);
                ptbPromotionImage.Image = img;
                txtPromotionImage.Text = ofd.FileName;
                ptbPromotionImage.SizeMode = PictureBoxSizeMode.Zoom;
            }
            catch (Exception ex)
            {
                _status.ERROR(ex.Message);
            }
        }

        private void btbAddPromotion_Click(object sender, EventArgs e)
        {
            try
            {
                string decr = txtDescription.Text;
                byte[] banner = _imageTool.ImageToByte(txtPromotionBanner.Text);
                if (_hotel.PromotionAdd(dtpStart.Value, dtpEnd.Value, txtPromotionTitle.Text, decr, banner))
                {
                    _status.SUCCESS("Add Promotion");
                    ViewPromotion();
                }
            }
            catch (Exception ex)
            {
                AbortServiceClient();
                _status.ERROR(ex.Message);
            }
        }

        private void btnDeletePromotion_Click(object sender, EventArgs e)
        {
            try
            {
                if (_hotel.PromotionRemove(int.Parse(lblPromotionId.Text)))
                {
                    _status.SUCCESS("Delete Promotion");
                    ViewPromotion();
                    return;
                }
                _status.FAIL("Delete Promotion");
            }
            catch (Exception ex)
            {
                AbortServiceClient();
                _status.ERROR(ex.Message);
            }
        }

        private void btnUpdatePromotion_Click(object sender, EventArgs e)
        {
            try
            {
                string decr = txtDescription.Text;
                byte[] banner = null;
                if (txtPromotionBanner.Text.Trim() != "")
                    banner = _imageTool.ImageToByte(txtPromotionBanner.Text);
                if (_hotel.PromotionModify(int.Parse(lblPromotionId.Text), dtpStart.Value, dtpEnd.Value,
                                           txtPromotionTitle.Text, decr, banner))
                {
                    _status.SUCCESS("Update Promotion");
                    ViewPromotion();
                    return;
                }
                _status.FAIL("Update Promotion");
            }
            catch (Exception ex)
            {
                AbortServiceClient();
                _status.ERROR(ex.Message);
            }
        }

        private void btnAddPromotionDetail_Click(object sender, EventArgs e)
        {
            try
            {
                string content = RtfToHtmlConverter.ConvertRtfToHtml(rtxPromotionContent.Rtf);
                byte[] image = _imageTool.ImageToByte(txtPromotionImage.Text);
                if (_hotel.PromotionDetailAdd(int.Parse(lblPromotionId.Text), content, image))
                {
                    _status.SUCCESS("Add Promotion Detail");
                    ViewPromotionDetail();
                    return;
                }
                _status.FAIL("Add Promotion Detail");
            }

            catch (Exception ex)
            {
                AbortServiceClient();
                _status.ERROR(ex.Message);
            }
        }


        private void ViewPromotionDetail()
        {
            try
            {
                List<vSubPromotionDetail> data = _hotel.PromotionDetailsGetByPromotionId(int.Parse(lblPromotionId.Text));
                dtgPromotionDetail.DataSource = data;
            }
            catch (Exception ex)
            {
                _status.ERROR(ex.Message);
            }
        }

        private void dtgPromotionDetail_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (dtgPromotionDetail.CurrentRow != null)
                {
                    int promotionDetailId =
                        int.Parse(dtgPromotionDetail.Rows[e.RowIndex].Cells[0].Value.ToString());
                    lblPromotionDetailId.Text = promotionDetailId.ToString();
                    PromotionDetail promotionDetail = _hotel.PromotionDetailGetById(promotionDetailId);
                    rtxPromotionContent.Rtf = HtmlToRtfConverter.ConvertHtmlToRtf(promotionDetail.PromotionContent);

                    ptbPromotionImage.Image = _imageTool.ByteToImage(promotionDetail.PromotionImage);
                    txtPromotionImage.Text = "";
                }
            }
            catch (Exception ex)
            {
                _status.ERROR(ex.Message);
            }
        }

        private void btnUpdatePromotinDetail_Click(object sender, EventArgs e)
        {
            try
            {
                string content = RtfToHtmlConverter.ConvertRtfToHtml(rtxPromotionContent.Rtf);
                byte[] image = null;
                if (txtPromotionImage.Text.Trim() != "")
                {
                    image = _imageTool.ImageToByte(txtPromotionImage.Text);
                }
                if (_hotel.PromotionDetailModify(int.Parse(lblPromotionDetailId.Text), content, image))
                {
                    _status.SUCCESS("Update Promotion Detail");
                    ViewPromotionDetail();
                    return;
                }
                _status.FAIL("Update Promotion Detail");
            }
            catch (Exception ex)
            {
                AbortServiceClient();
                _status.ERROR(ex.Message);
            }
        }

        private void btnDeletePromotionDetail_Click(object sender, EventArgs e)
        {
            try
            {
                if (_hotel.PromotionDetailRemove(int.Parse(lblPromotionDetailId.Text)))
                {
                    _status.SUCCESS("Delete Promotion Detail");
                    ViewPromotionDetail();
                    return;
                }
                _status.FAIL("Delete Promotion Detail");
            }

            catch (Exception ex)
            {
                AbortServiceClient();
                _status.ERROR(ex.Message);
            }
        }

        private void btnSearchPromotionTitle_Click(object sender, EventArgs e)
        {
            try
            {
                List<vSubPromotion> data = _hotel.PromotionsGetByTitle(txtPromotionTitle.Text);
                dtgPromotion.DataSource = data;
            }
            catch (Exception ex)
            {
                _status.ERROR(ex.Message);
            }
        }

        private void btnSearchPromotionDate_Click(object sender, EventArgs e)
        {
            try
            {
                List<vSubPromotion> data = _hotel.PromotionsGetByTime(dtpStart.Value, dtpEnd.Value);
                dtgPromotion.DataSource = data;
            }
            catch (Exception ex)
            {
                _status.ERROR(ex.Message);
            }
        }

        #endregion

        #region "REPORT"
        private void btnReportCustomerByID_Click(object sender, EventArgs e)
        {
            try
            {
                reportViewerHotel.Reset();
                reportViewerHotel.LocalReport.ReportEmbeddedResource = "BlueMarineWindowsFormsClient.Report.rptReportCustomerCheck.rdlc";
                List<vReportCustomerCheck> lstBill = _hotel.ReportCustomerCheck();
                reportViewerHotel.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", new BindingSource { DataSource = lstBill }));

                reportViewerHotel.RefreshReport();
            }
            catch (Exception ex)
            {
                _status.ERROR(ex.Message);
            }
        }

        private void btnReportIncomeByDate_Click(object sender, EventArgs e)
        {            
            try
            {
                DateTime dFrom = dtpFrom.Value.Date;
                DateTime dTo = dtpTo.Value.Date;

                reportViewerHotel.Reset();
                reportViewerHotel.LocalReport.ReportEmbeddedResource = "BlueMarineWindowsFormsClient.Report.rptIncome.rdlc";
                List<vIncome> lstIncome = _hotel.SelectIncomeByDate(dFrom, dTo);
                reportViewerHotel.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", new BindingSource { DataSource = lstIncome }));

                reportViewerHotel.RefreshReport();
            }
            catch (Exception ex)
            {
                _status.ERROR(ex.Message);
            }
        }
        #endregion
    }
}